// mircoApp/horizon/raisePrice/raisePrice.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    show: false,
    showType: '',
    page: 1,
    current_page: 0,
    last_page: 0,
    keyword: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  childEmit(e) {
    let {
      show,
      showType
    } = e.detail;
    this.setData({
      showType,
      show,
    })
    console.log(show, showType)
    if (!show && (showType == "sigleOption" || showType == "moreOption" || showType == "batch")) {
      this.setData({
        childGood: [],
        page: 1,
      })
      this.getData();
    }
  },
  showTap(e) {
    let {
      key,
      item
    } = e.currentTarget.dataset;

    if (key == 'option') {
      let {
        id
      } = e.currentTarget.dataset;
      this.getGoodDetail(id, item)
    } else {
      this.setData({
        show: true,
        showType: key
      })
    }
  },
  // 搜索
  onSearch() {
    this.setData({
      page: 1
    })
    this.getData();
  },
  onClick(e) {
    // let {
    //   name
    // } = e.currentTarget.dataset;
    // console.log(name,this.data.result,'ddddddddd')
    // let arr = this.data.result || [];
    // let array;
    // if (arr.length <= 0) {
    //   array = arr.push(String(name))
    // } 
    // else {
    //   if (arr.includes(String(name))) {
    //     arr.splice(arr.indexOf(String(name)), 1)
    //   } else {
    //     array = arr.push(String(name))
    //   }
    // }
    // this.setData({
    //   result: array
    // })
  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
    console.log(this.data.result, 'rrrrrrrr')
  },
  getGoodDetail(gid, item) {
    let urlStr = app.getNetAddresss("plugin.horizon.frontend.owner.countGoodsDetail");
    app._getNetWork({
      url: urlStr,
      data: {
        goods_id: gid
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let arr = res.data;
          let obj = {
            ...item,
            ...arr
          };
          if (arr.option_list.length <= 1 && arr.option_list[0].option_id == 0) {
            this.setData({
              showType: 'sigleOption'
            })
          } else {
            this.setData({
              showType: 'moreOption'
            })
          }
          console.log(obj)
          this.setData({
            childGood: obj,
            show: true
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.horizon.frontend.owner.countGoodsList");
    app._getNetWork({
      url: urlStr,
      data: {
        keyword: this.data.keyword,
        page: this.data.page
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          this.setData({
            current_page: res.data.current_page,
            last_page: res.data.last_page,
            infoData: res.data.data
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  switchChange(e) {
    console.log(e)
    let {
      item,
      ind
    } = e.currentTarget.dataset;
    let urlStr = app.getNetAddresss("plugin.horizon.frontend.owner.changeStatus");
    app._getNetWork({
      url: urlStr,
      data: {
        status: e.detail ? 1 : 0,
        goods_id: item.goods_id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            ['infoData[' + ind + '].open_status']: !item.open_status
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  }
})